home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / exampleCode / opengl / x+opengl / glxsimple.c < prev    next >
C/C++ Source or Header  |  1996-11-11  |  6KB  |  180 lines

  1. /* $Revision: 1.2 $ */
  2. /* compile: cc -o glxsimple glxsimple.c -lGL -lX11 */
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. #include <GL/glx.h>        /* this includes the necessary X headers */
  6. #include <GL/gl.h>
  7.  
  8. static int snglBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, None};
  9. static int dblBuf[] = {GLX_RGBA, GLX_DEPTH_SIZE, 16, GLX_DOUBLEBUFFER, None};
  10.  
  11. Display        *dpy;
  12. Window          win;
  13. GLfloat         xAngle = 42.0, yAngle = 82.0, zAngle = 112.0;
  14. GLboolean    doubleBuffer = GL_TRUE;
  15.  
  16. void
  17. fatalError(char *message)
  18. {
  19.     fprintf(stderr, "glxsimple: %s\n", message);
  20.     exit(1);
  21. }
  22.  
  23. void
  24. redraw(void)
  25. {
  26.     static GLboolean   displayListInited = GL_FALSE;
  27.  
  28.     if (displayListInited) {
  29.     /* if display list already exists, just execute it */
  30.     glCallList(1);
  31.     } else {
  32.     /* otherwise compile and execute to create the display list */
  33.     glNewList(1, GL_COMPILE_AND_EXECUTE);
  34.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  35.     /* front face */
  36.     glBegin(GL_QUADS);
  37.     glColor3f(0.0, 0.7, 0.1);    /* green */
  38.     glVertex3f(-1.0, 1.0, 1.0);
  39.     glVertex3f(1.0, 1.0, 1.0);
  40.     glVertex3f(1.0, -1.0, 1.0);
  41.     glVertex3f(-1.0, -1.0, 1.0);
  42.     /* back face */
  43.     glColor3f(0.9, 1.0, 0.0);    /* yellow */
  44.     glVertex3f(-1.0, 1.0, -1.0);
  45.     glVertex3f(1.0, 1.0, -1.0);
  46.     glVertex3f(1.0, -1.0, -1.0);
  47.     glVertex3f(-1.0, -1.0, -1.0);
  48.     /* top side face */
  49.     glColor3f(0.2, 0.2, 1.0);    /* blue */
  50.     glVertex3f(-1.0, 1.0, 1.0);
  51.     glVertex3f(1.0, 1.0, 1.0);
  52.     glVertex3f(1.0, 1.0, -1.0);
  53.     glVertex3f(-1.0, 1.0, -1.0);
  54.     /* bottom side face */
  55.     glColor3f(0.7, 0.0, 0.1);    /* red */
  56.     glVertex3f(-1.0, -1.0, 1.0);
  57.     glVertex3f(1.0, -1.0, 1.0);
  58.     glVertex3f(1.0, -1.0, -1.0);
  59.     glVertex3f(-1.0, -1.0, -1.0);
  60.     glEnd();
  61.     glEndList();
  62.     displayListInited = GL_TRUE;
  63.     }
  64.     if(doubleBuffer) glXSwapBuffers(dpy, win); /* buffer swap does implicit glFlush */
  65.        else glFlush(); /* explicit flush for single buffered case */
  66. }
  67.  
  68. void
  69. main(int argc, char **argv)
  70. {
  71.     XVisualInfo    *vi;
  72.     Colormap        cmap;
  73.     XSetWindowAttributes swa;
  74.     GLXContext      cx;
  75.     XEvent          event;
  76.     GLboolean       needRedraw = GL_FALSE, recalcModelView = GL_TRUE;
  77.     int            dummy;
  78.  
  79.     /*** (1) open a connection to the X server ***/
  80.  
  81.     dpy = XOpenDisplay(NULL);
  82.     if (dpy == NULL) fatalError("could not open display");
  83.  
  84.     /*** (2) make sure OpenGL's GLX extension supported ***/
  85.  
  86.     if(!glXQueryExtension(dpy, &dummy, &dummy)) fatalError("X server has no OpenGL GLX extension");
  87.  
  88.     /*** (3) find an appropriate visual ***/
  89.  
  90.     /* find an OpenGL-capable RGB visual with depth buffer */
  91.     vi = glXChooseVisual(dpy, DefaultScreen(dpy), dblBuf);
  92.     if (vi == NULL) {
  93.        vi = glXChooseVisual(dpy, DefaultScreen(dpy), snglBuf);
  94.        if (vi == NULL) fatalError("no RGB visual with depth buffer");
  95.        doubleBuffer = GL_FALSE;
  96.     }
  97.     if(vi->class != TrueColor) fatalError("TrueColor visual required for this program");
  98.  
  99.     /*** (4) create an OpenGL rendering context  ***/
  100.  
  101.     /* create an OpenGL rendering context */
  102.     cx = glXCreateContext(dpy, vi, /* no sharing of display lists */ None,
  103.               /* direct rendering if possible */ GL_TRUE);
  104.     if (cx == NULL) fatalError("could not create rendering context");
  105.  
  106.     /*** (5) create an X window with the selected visual ***/
  107.  
  108.     /* create an X colormap since probably not using default visual */
  109.     cmap = XCreateColormap(dpy, RootWindow(dpy, vi->screen), vi->visual, AllocNone);
  110.     swa.colormap = cmap;
  111.     swa.border_pixel = 0;
  112.     swa.event_mask = ExposureMask | ButtonPressMask | StructureNotifyMask;
  113.     win = XCreateWindow(dpy, RootWindow(dpy, vi->screen), 0, 0, 300, 300, 0, vi->depth,
  114.                         InputOutput, vi->visual, CWBorderPixel | CWColormap | CWEventMask, &swa);
  115.     XSetStandardProperties(dpy, win, "glxsimple", "glxsimple", None, argv, argc, NULL);
  116.  
  117.     /*** (6) bind the rendering context to the window ***/
  118.  
  119.     glXMakeCurrent(dpy, win, cx);
  120.  
  121.     /*** (7) request the X window to be displayed on the screen ***/
  122.  
  123.     XMapWindow(dpy, win);
  124.  
  125.     /*** (8) configure the OpenGL context for rendering ***/
  126.  
  127.     glEnable(GL_DEPTH_TEST); /* enable depth buffering */
  128.     glDepthFunc(GL_LESS);    /* pedantic, GL_LESS is the default */
  129.     glClearDepth(1.0);       /* pedantic, 1.0 is the default */
  130.     /* frame buffer clears should be to black */
  131.     glClearColor(0.0, 0.0, 0.0, 0.0);
  132.     /* set up projection transform */
  133.     glMatrixMode(GL_PROJECTION);
  134.     glLoadIdentity();
  135.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  136.     /* establish initial viewport */
  137.     glViewport(0, 0, 300, 300); /* pedantic, full window size is default viewport */
  138.  
  139.     /*** (9) dispatch X events ***/
  140.  
  141.     while (1) {
  142.     do {
  143.         XNextEvent(dpy, &event);
  144.         switch (event.type) {
  145.         case ButtonPress:
  146.         recalcModelView = GL_TRUE;
  147.         switch (event.xbutton.button) {
  148.         case 1: xAngle += 10.0; break;
  149.         case 2: yAngle += 10.0; break;
  150.         case 3: zAngle += 10.0; break;
  151.         }
  152.         break;
  153.         case ConfigureNotify:
  154.         glViewport(0, 0, event.xconfigure.width, event.xconfigure.height);
  155.         /* fall through... */
  156.         case Expose:
  157.         needRedraw = GL_TRUE;
  158.         break;
  159.         }
  160.     } while(XPending(dpy)); /* loop to compress events */
  161.     if (recalcModelView) {
  162.         glMatrixMode(GL_MODELVIEW);
  163.         /* reset modelview matrix to the identity matrix */
  164.         glLoadIdentity();
  165.         /* move the camera back three units */
  166.         glTranslatef(0.0, 0.0, -3.0);
  167.         /* rotate by X, Y, and Z angles */
  168.         glRotatef(xAngle, 0.1, 0.0, 0.0);
  169.         glRotatef(yAngle, 0.0, 0.1, 0.0);
  170.         glRotatef(zAngle, 0.0, 0.0, 1.0);
  171.         recalcModelView = GL_FALSE;
  172.         needRedraw = GL_TRUE;
  173.     }
  174.     if (needRedraw) {
  175.         redraw();
  176.         needRedraw = GL_FALSE;
  177.     }
  178.     }
  179. }
  180.